package com.example.hcbmsm.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.example.hcbmsm.domain.Pat;
import com.example.hcbmsm.domain.PatExample;
import com.example.hcbmsm.mapper.PatMapper;
import com.example.hcbmsm.service.PatService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.example.hcbmsm.util.JavaWebToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/10/29 18:55
 */
@Service
public class PatServiceImpl implements PatService {
    @Autowired
    private PatMapper patMapper;
    @Override
    public Map register(Pat pat) {
        PatExample patExample = new PatExample();
        System.out.println(pat.getPatientName());
        System.out.println(patExample.createCriteria().andPatientNameEqualTo(pat.getPatientName()));
        patExample.createCriteria().andPatientNameEqualTo(pat.getPatientName());
        if (!patMapper.selectByExample(patExample).isEmpty()) {
            return HCBMSMResult.WARNING(300, "用户名已存在");
        }
        patExample.createCriteria().andPatientUsernameEqualTo(pat.getPatientUsername());
        if (!patMapper.selectByExample(patExample).isEmpty()) {
            return HCBMSMResult.WARNING(301, "登录名已存在");
        }
        patExample.createCriteria().andPatientIdnumEqualTo(pat.getPatientIdnum());
        if ( !patMapper.selectByExample(patExample).isEmpty()) {
            return HCBMSMResult.WARNING(302, "身份证已被注册");
        }
        patExample.createCriteria().andMedicalInumEqualTo(pat.getMedicalInum());
        if (!patMapper.selectByExample(patExample).isEmpty()) {
            return HCBMSMResult.WARNING(303, "医保号码已被注册");
        }
        pat.setPatientPassword(DigestUtils.md5DigestAsHex(pat.getPatientPassword().getBytes()));
        if (patMapper.insertSelective(pat) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map getUser(String token) {
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long patId = new Long(tokenInfo.get("patId").asString());
        Pat pat = patMapper.selectByPrimaryKey(patId);
        pat.setPatientPassword("");
        return HCBMSMResult.SUCCESS(pat);
    }

    @Override
    public Map update(Pat pat) {
        PatExample patExample = new PatExample();
        patExample.createCriteria().andPatientUsernameEqualTo(pat.getPatientUsername());
        List<Pat> pats = patMapper.selectByExample(patExample);
        if (!pats.isEmpty() && !Objects.equals(pats.get(0).getPatientId(), pat.getPatientId())) {
            return HCBMSMResult.WARNING(301, "登录名已存在");
        }
        pat.setModifyTime(new Date());
        return patMapper.updateByPrimaryKeySelective(pat) == 1 ? HCBMSMResult.SUCCESS() : HCBMSMResult.ERROR();
    }
}
